package dataextraction;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;



public class ScienceDirectEngineLoader extends AbstractEngineLoader{

	private static final String SCIENCE_DIRECT_PUBLISHER = "SCIENCE_DIRECT";

	private static ScienceDirectEngineLoader instance;
	
	private static int NUM_OF_ENTRIES = 1; 
		
	public static ScienceDirectEngineLoader getInstance() {
		if (ScienceDirectEngineLoader.instance == null) {
			ScienceDirectEngineLoader.instance = new ScienceDirectEngineLoader();
		}
		return ScienceDirectEngineLoader.instance;
	}
	
	private ScienceDirectEngineLoader() {
		
	}

	
	private Entry createEntry(String title, String url, String publicationAbstract, String[] pages){
			   
	    Entry entry = new Entry();
	    
    	entry.setDocumentTitle(title.substring(1,title.length()-1));//remove aspas inicio/fim
    	
    	String endPage = pages[1].substring(0,pages[1].length()-1);
    	String startPage = pages[0].substring(1);
    	try{
    		entry.setNumberOfPages(Integer.parseInt(endPage)-Integer.parseInt(startPage)+1);
    	}catch(NumberFormatException e){
    		entry.setNumberOfPages(-1);//paginas final/inicial nao definida
    	}
    	entry.setPublicationAbstract(publicationAbstract.substring(1,publicationAbstract.length()-1));//remove aspas inicio/fim
    	entry.setPdfLink(url);
    	entry.setPublisher(SCIENCE_DIRECT_PUBLISHER);
    	entry.setId("A_"+SCIENCE_DIRECT_PUBLISHER+"_"+NUM_OF_ENTRIES++);
	    
	    return entry;
	}
	
	
	public List<Entry> reader(File csvFile) throws IOException {

		FileInputStream stream = new FileInputStream(csvFile);  
        InputStreamReader streamReader = new InputStreamReader(stream);  
        BufferedReader reader  = new BufferedReader(streamReader);
        
        List<Entry> entriesOnFile = new ArrayList<Entry>();
        
        String line = null;
        String title = null;
        String url = null;
        String publicationAbstract = null;
        
        String[] pages = null;
              
        while ((line = reader.readLine()) != null) {  
        	try{
	        	if(line.substring(0,8).equals("title = ")){
	        		
	        		if(title != null){
	        			System.err.println("Adicionar entrada de forma manual: "+title);
	    		    	NUM_OF_ADD_MANUAL++;
	        		}
	        		
	        		title = line.substring(8, line.length()-1);
	        	}else if(line.substring(0,6).equals("url = ")){
	        		url = line.substring(6, line.length()-1);
	        	}else if(line.substring(0,11).equals("abstract = ")){
	        		publicationAbstract = line.substring(11);
	        	}else if(line.substring(0,8).equals("pages = ")){
	        		String content = line.substring(8, line.length()-1);
	        		pages = content.split(" - ");
	        	}
        	}catch(StringIndexOutOfBoundsException e){
        		
        	}
        	
        	if(title != null && url != null && publicationAbstract != null){
        		Entry entry = createEntry(title,url,publicationAbstract,pages);
        		
        		
                if(entry.getDocumentTitle() != null && entry.getPublicationAbstract() != null){
                	entriesOnFile.add(entry);
                }
               title = null;
               url = null;
               publicationAbstract = null;
        	}
            
        }  
        
        reader.close();
        streamReader.close();
        stream.close();
		
        
        System.out.println("Fim do arquivo "+ csvFile.getName() + " # de entradas: "+entriesOnFile.size());
       
        
        return entriesOnFile;
	}

	@Override
	protected String getPublisher() {
		return SCIENCE_DIRECT_PUBLISHER;
	}
	
}
